<?php

/**
 * accueil actions.
 *
 * @package    Colibri
 * @subpackage Page de demarrage
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class touchAppActions extends baseDossierActions{
   
  public function executeUserHome(sfWebRequest $request){
  	$this->personne = $this->getUser()->getUserProfile();
  	$q = Doctrine_Query::create()->from('service s')->orderBy('s.description');
  	$q->setHydrationMode(Doctrine::HYDRATE_ARRAY);
  	$this->services = $q->execute();
  	/*$q = Doctrine_Query::create()->from('notification n')
  			->where('n.viewed = false')
  			->innerJoin('n.message m WITH n.evenementclass = ? ', 'message')
  			->innerJoin('m.service s')
  			->addOrderBy('s.description')
  			->addWhere('n.personne_id = ?', $this->personne['id']);
	$q->setHydrationMode(Doctrine::HYDRATE_ARRAY);  			
  	$this->messages = $q->execute();*/
  }

  public function executeListDossiers(sfWebRequest $request){
  	$this->personne = $this->getUser()->getUserProfile();
	$i18N = sfContext::getInstance()->getI18N();
	$user = $this->getUser();

	$this->view = $request->getParameter('view');
	
	$viewOriginal = $this->getUser()->getAttribute('viewOriginal');
	if($viewOriginal!=$this->view){
		$this->getUser()->getAttributeHolder()->remove('activeFilters');
		$this->getUser()->setAttribute('viewOriginal',$this->view);
	}

	$this->objDef = new objectDef('dossier');
	$this->config = ambnoyFuncs::getViewConfig($this->view, $this->personne, $this->objDef);
	
	$this->actualPage = $request->getParameter('page',1);
	$this->getUser()->setAttribute('viewPage',$this->actualPage);
	$this->order = $request->getParameter('order', $this->config['orderColumn']);
	
	
  }
    
  public function executeListMessages(sfWebRequest $request){
  	$this->view = $request->getParameter('view');
  	$this->module = 'messages';
  	$this->personne = $this->getUser()->getUserProfile();
	$i18N = sfContext::getInstance()->getI18N();
	$user = $this->getUser();

	$this->view = $request->getParameter('view');
	
	$viewOriginal = $this->getUser()->getAttribute('viewOriginal');
	if($viewOriginal!=$this->view){
		$this->getUser()->getAttributeHolder()->remove('activeFilters');
		$this->getUser()->setAttribute('viewOriginal',$this->view);
	}

	$this->objDef = new objectDef('message');
	$this->config = ambnoyFuncs::getViewConfig($this->view, $this->personne, $this->objDef);
	
	$this->actualPage = $request->getParameter('page',1);
	$this->order = $request->getParameter('order', $this->config['orderColumn']);

	$this->getUser()->setAttribute('lastviewinfo', array('actualPage' => $this->actualPage, 'order' => $this->order, 'view' => $this->view));
	
  	
  }
  
  public function executeChoixFormulaireDef(sfWebRequest $request){
  	//$idPersonne = $request->getParameter('personne');
  	$idService = $request->getParameter('service');
  	$this->personne = $this->getUser()->getUserProfile();
  	// $this->personne = Doctrine::getTable('personne')->find($idPersonne);
  	$this->service = Doctrine::getTable('service')->find($idService);
  	
  	//Verifier s'il faut demander pour un formulairedef
  	
  	$q = Doctrine_Query::create()->from('formulairedef fd')
	  	->innerJoin('fd.type t')
	  	->innerJoin('t.service s')
	  	->where('s.id = ?', $this->service['id'])
	  	->orderBy('fd.description');
  	$q->setHydrationMode(Doctrine::HYDRATE_ARRAY);
  	$this->formulairesDef = $q->execute();
  	if(count($this->formulairesDef) == 1 ){
  		$this->redirect('@createDossier?' . '&formulaireDef=' . $this->formulairesDef[0]['id']);
  	}
  	
  }
  
  public function executeCreateDossier(sfWebRequest $request){
  	$idFormulaireDef = $request->getParameter('formulaireDef');
  	$idDossier = $request->getParameter('idDossier', 0);
  	$idPersonne = $request->getParameter('demandeur', false);
  	if($idPersonne){
  		$this->personne = Doctrine::getTable('personne')->find($idPersonne);
  	}else{
  		$this->personne = $this->getUser()->getUserProfile();
  	}
  	if($idDossier){
  		$dossier = Doctrine::getTable('dossier')->find($idDossier);
  		$this->formulaireDef = $dossier->formulairedef;
  	}else{
  		$this->formulaireDef = Doctrine::getTable('formulairedef')->find($idFormulaireDef);	
  	}
  	$this->className = $this->formulaireDef->xtable;
  	$formClass = $this->className . 'Form';
  	if($idDossier){
  		$this->form = new $formClass($dossier->getDetail());
  	}else{
  		$this->form = new $formClass;
  		
  	}
  	
  	$this->objDef = new objectDef($this->formulaireDef->xtable);

  	$this->module = $this->getModuleName();
	$this->itemsForms = array();
  	if($this->objDef->hasAttach){
		$this->formAttach = new attachmentForm();
		$this->getUser()->setAttribute('filesToUpload', array());
	}else{
		$this->formAttach = null;
	}  	
  }

  public function executeSaveDossier(sfWebRequest $request){
  	$this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    
  	$idFormulaireDef = $request->getParameter('formulaireDef');
  	$idPersonne = $request->getParameter('demandeur', false);
  	if($idPersonne){
  		$this->personne = Doctrine::getTable('personne')->find($idPersonne);
  	}else{
  		$this->personne = $this->getUser()->getUserProfile();
  	}
  	$this->formulaireDef = Doctrine::getTable('formulairedef')->find($idFormulaireDef);
  	$this->className = $this->formulaireDef->xtable;
  	$formClass = $this->className . 'Form';  	
  	
  	$id = $request->getParameter('id', false);
  	if($id){
  		$this->forward404Unless($self = Doctrine::getTable($this->className)->find($id), sprintf('Object de la class %s does not exist (%s).', $this->className, $id));
  		$this->form = new $formClass($self);	
  	}else{
  		$this->form = new $formClass();
  	}
    
 	$file = false;
	$params = $request->getParameter($this->form->getName());

  	$util = new Util;
	$filesToUpld = array();
	$this->objDef = new objectDef($this->formulaireDef->xtable);
	foreach($this->objDef->attributes as $fieldName => $att){
		if($att['show']){
			if($att['type']=='date'){
				$params[$fieldName] = $util->getDateFromStr($params[$fieldName]);
			}elseif($att['type']=='timestamp'){
				$params[$fieldName] = $util->getDateFromStr($params[$fieldName], true);
			}elseif($att['type']=='file' or $att['type']=='image'){
				 $file = true;
				 $filesToUpld[]=$fieldName;
			}
		}
	}

	if($this->form->isMultipart()){
		$this->form->bind($params, $request->getFiles($this->form->getName()));
  	}else{
		$this->form->bind($params);
	}
    if ($this->form->isValid())
    {
    	$this->dossier = ambnoyFuncs::updateDossier($this->formulaireDef, $this->personne, $this->form );
	    
	    $this->redirect('touchApp/thankyou?dossier='.$this->dossier->id);	
    }
    else
    {
    	$util->sendDebug('Invalid Form: ', 2);
		$i18N = sfContext::getInstance()->getI18N();
    	$errors = '';
    	if($this->form->renderGlobalErrors()!=''){
    		$errors = $i18N->__('Form Errors: ').$this->form->renderGlobalErrors().'<br />';
    	}
   		 foreach($this->objDef->attributes as $fieldName => $att){
    		if($att['type']!='List' and $att['type']!='function' and $att['type']!='BOList' and $att['type']!='CheckboxList'){
	    		if($att['type']=='BO' or $att['type']=='BOList'){$fieldName = $fieldName.'_id';}
	    		if (isset($this->form[$fieldName]) and $this->form[$fieldName]->hasError()){ 
	    			$errors .= $i18N->__('Error in the field : ') . $att['label'].' ('.$fieldName.')';
		    		foreach ($this->form[$fieldName]->getError() as $error){ 
			    			$errors .= ' * '.$error .'<br />';
		    		}
	    		}
    		}
    	}
    	$util->sendDebug('Form Errors: ' . $errors, 2);
    	//echo $errors;
    	//renderError() 
    	
    	$this->getUser()->setFlash('Error', $i18N->__('Invalid Form:'). '<br />'.$errors);
    	
    }
    
     
    
  	$this->module = $this->getModuleName();
	$this->itemsForms = array();
  	if($this->objDef->hasAttach){
		$this->formAttach = new attachmentForm();
		$this->getUser()->setAttribute('filesToUpload', array());
	}else{
		$this->formAttach = null;
	}
	$this->setTemplate('createDossier');
	  	
  }
  
  public function executeThankyou(sfWebRequest $request){
 		$this->dossier = Doctrine::getTable('dossier')->find($request->getParameter('dossier'));
 		$this->objDef = new objectDef('dossier');
 		$this->detailObjDef = new objectDef($this->dossier->formulairedef->xtable);
  }

 public function executeShowDossier(sfWebRequest $request){
 		$this->dossier = Doctrine::getTable('dossier')->find($request->getParameter('dossier'));
 		$this->personne = $this->getUser()->getUserProfile();
 		/*Get all this parameters in order to go back to the same view I was.*/
 		$this->view = $request->getParameter('view');
		$this->objDef = new objectDef('dossier');
		$this->config = ambnoyFuncs::getViewConfig($this->view, $this->personne, $this->objDef);
		$this->actualPage = $request->getParameter('page',1);
		$this->order = $request->getParameter('order', $this->config['orderColumn']);
 		
 		
 		$this->detailObjDef = new objectDef($this->dossier->formulairedef->xtable);
  }
  
  public function executeSubmitThankyou(sfWebRequest $request){
  	
  	$this->redirect('@userHome');  	
  } 
  
}